<?php

namespace App\Modules\Api\Http\Controllers;

use App\Modules\Api\Http\Controllers\BaseController;
use App\Services\Api\UserService;
use Illuminate\Http\Request;

// 制定允许其他域名访问
class UserController extends BaseController
{

    /**
     * 用户登录
     * @param Request $request
     * @method login
     * @author chengzhigang<1256699215@qq.com>
     */
    public function login(Request $request)
    {
        $phone = $request->input('phone');
        $password = $request->input('password');
        $type = $request->input('type');
        $code = $request->input('code');
        if (empty($phone) || !checkPhone($phone)) {
            return apiResponse('请输入正确手机号码');
        }
        if ($type == 1) {
            //验证码登录
            if (empty($code)) {
                return apiResponse('请输入验证码');
            }
        } else {
            if (empty($password)) {
                return apiResponse('请输入密码');
            }
        }
        $userService = new UserService();
        $result = $userService->login($phone, $password, $code, $type);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 用户注册
     * @param Request $request
     * @method register
     * @author chengzhigang<1256699215@qq.com>
     */
    public function register(Request $request)
    {
        $phone = $request->input('phone');
        $password = $request->input('password');
        $code = $request->input('code');
        if (empty($phone) || !checkPhone($phone)) {
            return apiResponse('请输入正确手机号码');
        }
        if (empty($code)) {
            return apiResponse('请输入验证码');
        }
        if (empty($password)) {
            return apiResponse('请输入密码');
        }
        $userService = new UserService();
        $result = $userService->register($phone, $password, $code);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 忘记密码
     * @param Request $request
     * @method forgetPwd
     * @author chengzhigang<1256699215@qq.com>
     */
    public function forgetPwd(Request $request)
    {
        $phone = $request->input('phone');
        $password = $request->input('password');
        $code = $request->input('code');
        if (empty($phone) || !checkPhone($phone)) {
            return apiResponse('请输入正确手机号码');
        }
        if (empty($code)) {
            return apiResponse('请输入验证码');
        }
        if (empty($password)) {
            return apiResponse('请输入新密码');
        }
        $userService = new UserService();
        $result = $userService->forgetPwd($phone, $password, $code);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 修改密码
     * @param Request $request
     * @method updatePwd
     * @author chengzhigang<1256699215@qq.com>
     */
    public function updatePwd(Request $request)
    {
        $token = $request->input('token');
        $oldPassword = $request->input('oldPassword');
        $password = $request->input('password');
        $confirmPassword = $request->input('confirmPassword');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($oldPassword)) {
            return apiResponse('请输入原密码');
        }
        if (empty($password)) {
            return apiResponse('请输入密码');
        }
        if (empty($confirmPassword)) {
            return apiResponse('请输入确认密码');
        }
        if ($password != $confirmPassword) {
            return apiResponse('确认密码不正确');
        }
        $userService = new UserService();
        $result = $userService->updatePwd($token, $oldPassword, $password);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 上传头像
     * @param Reqeuest $request
     * @method uploadAvatar
     * @author chengzhigang<1256699215@qq.com>
     */
    public function uploadAvatar(Request $request)
    {
        $file = $request->file('image');
        $token = $request->input('token');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $userService = new UserService();
        $result = $userService->uploadAvatar($token, $file);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 更新用户信息
     * @param Request $request
     * @method updateInfo
     * @author chengzhigang<1256699215@qq.com>
     */
    public function updateInfo(Request $request)
    {
        $token = $request->input('token');
        $sign = $request->input('sign');
        $nickname = $request->input('nickname');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $data = [];
        if (!empty($nickname)) {
            $data['nickname'] = $nickname;
        }
        if (!empty($sign)) {
            $data['sign'] = $sign;
        }
        $userService = new UserService();
        $result = $userService->updateInfo($token, $data);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * QQ第三方登录
     * @param Request $request
     * @method qqLogin
     * @author chengzhigang<1256699215@qq.com>
     */
    public function qqLogin(Request $request)
    {
        $nickname = $request->input('nickname');
        $openId = $request->input('openId');
        $avatar = $request->input('avatar');
        $sex = $request->input('sex');
        $userService = new UserService();
        $result = $userService->qqLogin($openId, $nickname, $avatar, $sex);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 绑定手机号
     * @param Request $request
     * @method bindPhone
     * @author chengzhigang<1256699215@qq.com>
     */
    public function bindPhone(Request $request)
    {
        $phone = $request->input('phone');
        $token = $request->input('token');
        $code = $request->input('code');
        if (empty($phone) || !checkPhone($phone)) {
            return apiResponse('请输入正确手机号码');
        }
        if (empty($code)) {
            return apiResponse('请输入验证码');
        }
        $userService = new UserService();
        $result = $userService->bindPhone($token, $phone, $code);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 绑定QQ
     * @param Request $request
     * @method bindQQ
     * @author chengzhigang<1256699215@qq.com>
     */
    public function bindQQ(Request $request)
    {
        $token = $request->input('token');
        $nickname = $request->input('nickname');
        $openId = $request->input('openId');
        $avatar = $request->input('avatar');
        $sex = $request->input('sex');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $userService = new UserService();
        $result = $userService->bindQQ($token, $openId, $nickname, $avatar, $sex);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 注销登录
     * @param Request $request
     * @method logout
     * @author chengzhigang<1256699215@qq.com>
     */
    public function logout(Request $request)
    {
        $token = $request->input('token');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $userService = new UserService();
        $result = $userService->logout($token);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 通过手机号查询用户
     * @param Request $request
     * @method getUserByPhone
     * @author chengzhigang<1256699215@qq.com>
     * @param
     * @return
     */
    public function getUserByPhone(Request $request)
    {
        $token = $request->input('token');
        $phone = $request->input('phone');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($phone)) {
            return apiResponse('请输入手机号');
        }
        $userService = new UserService();
        $result = $userService->getUserByPhone($token, $phone);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    ####################设置#################

    /**
     * 意见反馈
     * @param Request $request
     * @method addFeedback
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addFeedback(Request $request)
    {
        $token = $request->input('token');
        $contact = $request->input('contact');
        $type = $request->input('type');
        $content = $request->input('content');
        $images = $request->file('image');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($type)) {
            return apiResponse('请选择分类标签');
        }
        if (empty($content)) {
            return apiResponse('请输入反馈内容');
        }
        if (count($images) > 5) {
            return apiResponse('图片不能超过5张');
        }
        $userService = new UserService();
        $result = $userService->addFeedback($token, $type, $content, $contact, $images);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    ############收货地址###########
    /**
     * Undocumented function
     * @param Request $request
     * @method getDefaultAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getDefaultAddress(Request $request)
    {
        $token = $request->input('token');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $userService = new UserService();
        $result = $userService->getDefaultAddress($token);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 获取用户地址
     * @param Request $request
     * @method getUserAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getUserAddress(Request $request)
    {
        $token = $request->input('token');
        $address_id = $request->input('address_id');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($address_id)) {
            return apiResponse('地址id不能为空');
        }
        $userService = new UserService();
        $result = $userService->getUserAddress($token, $address_id);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 用户地址列表
     * @param Request $request
     * @method addressList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addressList(Request $request)
    {
        $token = $request->input('token');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        $userService = new UserService();
        $result = $userService->addressList($token);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 新增收货地址
     * @param Request $request
     * @method addAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addAddress(Request $request)
    {
        $token = $request->input('token');
        $consignee = $request->input('consignee');
        $phone = $request->input('phone');
        $province = $request->input('province', '');
        $city = $request->input('city', '');
        $district = $request->input('district', '');
        $address = $request->input('address', '');
        $is_default = $request->input('is_default', 1);
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($consignee)) {
            return apiResponse('请填写收货人姓名');
        }
        if (empty($phone)) {
            return apiResponse('请填写收货人手机号');
        }
        if (!checkPhone($phone)) {
            return apiResponse('手机号格式不正确');
        }
        if (empty($province)) {
            return apiResponse('请填写所在地区');
        }
        if (empty($address)) {
            return apiResponse('请填写详细地址');
        }
        $userService = new UserService();
        $result = $userService->addAddress($token, $consignee, $phone, $province, $city, $district, $address, $is_default);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 编辑收货地址
     * @param Request $request
     * @method editAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function editAddress(Request $request)
    {
        $token = $request->input('token');
        $consignee = $request->input('consignee');
        $phone = $request->input('phone');
        $province = $request->input('province', '');
        $city = $request->input('city', '');
        $district = $request->input('district', '');
        $address = $request->input('address', '');
        $is_default = $request->input('is_default', 1);
        $address_id = $request->input('address_id');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($address_id)) {
            return apiResponse('收货地址id不能为空');
        }
        if (empty($consignee)) {
            return apiResponse('请填写收货人姓名');
        }
        if (empty($phone)) {
            return apiResponse('请填写收货人手机号');
        }
        if (!checkPhone($phone)) {
            return apiResponse('手机号格式不正确');
        }
        if (empty($province)) {
            return apiResponse('请填写所在地区');
        }
        if (empty($address)) {
            return apiResponse('请填写详细地址');
        }
        $userService = new UserService();
        $result = $userService->editAddress($token, $consignee, $phone, $province, $city, $district, $address, $is_default, $address_id);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 设置默认地址
     * @param Request $request
     * @method setDefaultAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function setDefaultAddress(Request $request)
    {
        $token = $request->input('token');
        $address_id = $request->input('address_id');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($address_id)) {
            return apiResponse('收货地址id不能为空');
        }
        $userService = new UserService();
        $result = $userService->setDefaultAddress($token, $address_id);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }

    /**
     * 删除收货地址
     * @param Request $request
     * @method delAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function delAddress(Request $request)
    {
        $token = $request->input('token');
        $address_id = $request->input('address_id');
        if (empty($token)) {
            return apiResponse('请登录应用');
        }
        if (empty($address_id)) {
            return apiResponse('收货地址id不能为空');
        }
        $userService = new UserService();
        $result = $userService->delAddress($token, $address_id);
        return apiResponse($result['msg'], $result['status'], $result['data']);
    }
}
